package offer;

public class Solution43 {
    public int countDigitOne(int n) {
        int res = 0;
        for (long k = 1; k <= n; k *= 10) {
            res += (n / (k * 10)) * k + Math.min(Math.max(n % (k * 10) - k + 1, 0), k);
        }
        return res;
    }
}
